﻿#region Copyright (c) 2012-10, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


namespace Amarok
{
	/// <summary>
	/// </summary>
	public static class TestHelper
	{
		/// <summary>
		/// </summary>
		public static Boolean IsWindowsVista
		{
			get
			{
				var version = Environment.OSVersion.Version;
				return version.Major == 6 && version.Minor == 0;
			}
		}


		/// <summary>
		/// </summary>
		public static void TestDuration(String name, Action testAction)
		{
			Console.WriteLine();
			Console.WriteLine("*** {0}:", name);

			var sw = Stopwatch.StartNew();
			testAction();
			sw.Stop();

			Console.WriteLine("Time = {0} ms", sw.Elapsed.TotalMilliseconds);
		}

		/// <summary>
		/// </summary>
		public static void TestPerformance(String name, Int32 loops, Action<Int32> testAction)
		{
			TestPerformance(name, loops, 1, testAction);
		}

		/// <summary>
		/// </summary>
		public static void TestPerformance(String name, Int32 loops, Int32 multiplier, Action<Int32> testAction)
		{
			var sw = Stopwatch.StartNew();
			for (Int32 i = 0; i < loops; i++)
				testAction(i);
			sw.Stop();

			var count = loops * multiplier;

			Console.WriteLine();
			Console.WriteLine("*** {0}:", name);
			Console.WriteLine("Count = {0}", count);
			Console.WriteLine("Time = {0} ms", sw.Elapsed.TotalMilliseconds);
			Console.WriteLine("Count/ms = {0} 1/ms", count / sw.Elapsed.TotalMilliseconds);
			Console.WriteLine("Time/Call = {0} µs", sw.Elapsed.TotalMilliseconds / count * 1000);
		}

		/// <summary>
		/// </summary>
		public static T TestBinaryFormatterRoundtrip<T>(T input)
		{
			using (var stream = new MemoryStream())
			{
				var formatter = new BinaryFormatter();
				formatter.Serialize(stream, input);
				stream.Position = 0;
				return (T)formatter.Deserialize(stream);
			}
		}

		/// <summary>
		/// </summary>
		public static T TestDataContractRoundtrip<T>(T input, params Type[] knownTypes)
		{
			using (var stream = new MemoryStream())
			{
				var serializer = new DataContractSerializer(typeof(T), knownTypes);
				serializer.WriteObject(stream, input);
				stream.Position = 0;
				return (T)serializer.ReadObject(stream);
			}
		}

	}
}

#endif
